//
//  OMHomePageViewController.m
//  ERP
//
//  Created by QA on 15/9/18.
//  Copyright (c) 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "OMHomePageViewController.h"
#import "GEProgressCollectionViewCell.h"
#import "OMHomePageSmailViewCell.h"

#import "Charts-Swift.h"
#import "NSDictionary+DataFormat.h"
#import "DateTimeFormat.h"
#import "GEWeekTimeFromatter.h"

#import "JCOrderListViewController.h"
#import "OMCreateOrderViewController.h"
#import "StringAxisValueFormatter.h"

static NSString *const CellIdentifierSmail = @"OMHomePageSmailViewCell";
static NSString *const HeaderIdentifier = @"UICollectionViewCell";
static NSString *const progressIdentifier = @"GEProgressCollectionViewCell";
static NSString *const RAMCollectionViewFlemishBondHeaderKind = @"RAMCollectionViewFlemishBondHeaderKind";

@interface OMHomePageViewController ()

@property (nonatomic, strong) NSMutableArray *photosArray;

@end

@implementation OMHomePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"手机订单"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.collectionView registerClass:[OMHomePageSmailViewCell class] forCellWithReuseIdentifier:CellIdentifierSmail];
    [self.collectionView registerClass:[GEProgressCollectionViewCell class] forCellWithReuseIdentifier:progressIdentifier];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:HeaderIdentifier];
}

#pragma mark UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(SuperWidth - 10, 300);
    } else {
        return CGSizeMake((SuperWidth - 15)/2, 92);
    }

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if(section == 0){
        return UIEdgeInsetsMake(5.f, 5.f, 0.f, 5.f);
    } else {
        return UIEdgeInsetsMake(5.f, 5.f, 5.f, 5.f);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //绘制进度条
        GEProgressCollectionViewCell *progressBarCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:progressIdentifier forIndexPath:indexPath];
        if (progressBarCell.objectValue == nil) {
            [progressBarCell getNetWorkDataTableView:collectionView moduleType:@"DDGL_DDLY_CHART" parameter:nil index:indexPath.section];
            progressBarCell.backgroundColor = [UIColor whiteColor];
            return progressBarCell;
        }
        
        //综合分析报表
        UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:HeaderIdentifier forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor whiteColor];
        if(cell.contentView.subviews.count <= 0) [self drawContractChartView:cell data:progressBarCell.objectValue];
        return cell;
        
    } else {
        //合同功能选项
        if (indexPath.item == 0) {
            OMHomePageSmailViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifierSmail forIndexPath:indexPath];
            cell.titleLable.text = @"内部下单";
            cell.describeLable.text = @"内部自主下单";
            cell.imageView.image = [UIImage imageNamed:@"order_tj_icon"];
            return cell;
            
        } else if (indexPath.item == 1) {
            OMHomePageSmailViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifierSmail forIndexPath:indexPath];
            cell.titleLable.text = @"待审核订单";
            cell.describeLable.text = @"查看审核订单";
            cell.imageView.image = [UIImage imageNamed:@"order_sh_icon"];
            return cell;
            
        }  else if (indexPath.item == 2) {
            OMHomePageSmailViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifierSmail forIndexPath:indexPath];
            cell.titleLable.text = @"已拒绝订单";
            cell.describeLable.text = @"查看所有拒绝订单";
            cell.imageView.image = [UIImage imageNamed:@"order_jj_icon"];
            return cell;
            
        }  else if (indexPath.item == 3) {
            OMHomePageSmailViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifierSmail forIndexPath:indexPath];
            cell.titleLable.text = @"历史订单";
            cell.describeLable.text = @"查看所有订单";
            cell.imageView.image = [UIImage imageNamed:@"order_ls_icon"];
            return cell;
            
        }
        return nil;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //@审核状态（0未审核，1已审核, 2已拒绝，4待接单）
    if (indexPath.section == 0) return;
    if (indexPath.item == 0) { //内部下单
        OMCreateOrderViewController *viewController = [[OMCreateOrderViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.item == 1) { //订单审核
        JCOrderListViewController *viewController = [[JCOrderListViewController alloc] initWithAuditState:0];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.item == 2) { //拒绝订单
        JCOrderListViewController *viewController = [[JCOrderListViewController alloc] initWithAuditState:2];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.item == 3) { //历史订单
        JCOrderListViewController *viewController = [[JCOrderListViewController alloc] initWithAuditState:1];
        [self.navigationController pushViewController:viewController animated:YES];
        
    }
}

#pragma mark RadarChartView setData

- (void)drawContractChartView:(UICollectionViewCell*)cell data:(NSArray*)dataArray
{
    //NSArray *value = [dataArray firstObject];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth - 10, 30)];
    view.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:view];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SuperWidth - 10, 29.5)];
    lable.textColor = ThemeColorBlue;
    lable.font = [UIFont boldSystemFontOfSize:15];
    //lable.textAlignment = NSTextAlignmentCenter;
    lable.text = [NSString stringWithFormat:@"近7天订单走势分析"];
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth - 10, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    LineChartView *_chartView = [[LineChartView alloc] initWithFrame:CGRectMake(0, 40, SuperWidth - 10, 250)];
    //_chartView.delegate = self;
//    _chartView.descriptionText = @"";
    _chartView.noDataText = @"暂无数据";
    _chartView.userInteractionEnabled = NO;
    
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.dragEnabled = YES;
    [_chartView setScaleEnabled:YES];
    _chartView.pinchZoomEnabled = NO;
    [_chartView animateWithXAxisDuration:3.0 yAxisDuration:3.0];
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.labelPosition = XAxisLabelPositionBothSided;
    xAxis.labelFont = [UIFont systemFontOfSize:8.f];
    
    _chartView.rightAxis.labelFont = [UIFont systemFontOfSize:8.f];
    _chartView.leftAxis.labelFont = [UIFont systemFontOfSize:8.f];
    
    NSArray *xVals = [GEWeekTimeFromatter getBeforeWeekTimeForString];
    //设置底部显示文字
    _chartView.xAxis.granularity = 1.0; // only intervals
    _chartView.xAxis.valueFormatter = [[StringAxisValueFormatter alloc] initWithData:xVals];
    
    ChartLegend *l = _chartView.legend;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
    l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
    l.orientation = ChartLegendOrientationHorizontal;
    _chartView.data = [self setContractChartData:dataArray];
    [cell.contentView addSubview:_chartView];
}

- (LineChartData*)setContractChartData:(NSArray*)dataArray
{
    dataArray = dataArray.count > 0 ? [dataArray firstObject] : [NSArray array];
    
    NSArray *colors = @[ChartColorTemplates.joyful[0], ChartColorTemplates.joyful[1], ChartColorTemplates.joyful[3]];
    NSArray *lables = @[@"订单总量",@"手机订单量",@"网页订单量"];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    for (int z = 0; z < 3; z++)  {
        NSMutableArray *values = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < dataArray.count; i++)  {
            NSDictionary *strinDic = [dataArray objectAtIndex:i];
            double val = [strinDic objectFormatFloatForKey:lables[z]];
            [values addObject:[[ChartDataEntry alloc] initWithX:i y:val]];
        }
        
        LineChartDataSet *d = [[LineChartDataSet alloc] initWithValues:values label:lables[z]];
        d.lineWidth = 2.0;
        d.circleRadius = 3.0;
        
        UIColor *color = colors[z];
        [d setColor:color];
        [d setCircleColor:color];
        [d setValueTextColor:color];
        [dataSets addObject:d];
    }
    
    LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
    [data setValueFont:[UIFont systemFontOfSize:7.f]];
    
    return data;
}

#pragma mark
#pragma mark - UIAlertView

- (void)sendAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

@end
